package org.springblade.modules.app.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.app.entity.OrderDetail;
import org.springblade.modules.app.service.IOrderDetailService;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

//////////////order detail
@RestController
@AllArgsConstructor
@RequestMapping("blade-app/orderdetail")
public class OrderDetailController extends BladeController {

    private final IOrderDetailService orderDetailService;


    ////////order detail by order table primary key
    @GetMapping("/getOrderDetailList")
    public List<OrderDetail> getOrderDetailList(OrderDetail orderDetail) {
        LambdaQueryWrapper<OrderDetail> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.and(wrapper -> wrapper.eq(OrderDetail::getOrderid, orderDetail.getOrderid()));
        List<OrderDetail> orderDetailList = orderDetailService.list(queryWrapper);
        return orderDetailList;
    }


    /**
     * 详情
     */
    @GetMapping("/detail")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "详情", notes = "传入orderDetail")
    public R<OrderDetail> detail(OrderDetail orderDetail) {
        OrderDetail detail = orderDetailService.getOne(Condition.getQueryWrapper(orderDetail));
        return R.data(detail);
    }

    /**
     * 分页 订单明细
     */
    @GetMapping("/list")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "分页", notes = "传入orderDetail")
    public R<IPage<OrderDetail>> list(OrderDetail orderDetail, Query query) {
        IPage<OrderDetail> pages = orderDetailService.page(Condition.getPage(query), Condition.getQueryWrapper(orderDetail));
        return R.data(pages);
    }

    /**
     * 分页 订单明细
     */
    @GetMapping("/page")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "分页", notes = "传入orderDetail")
    public R<IPage<OrderDetail>> page(OrderDetail orderDetail, Query query) {
        IPage<OrderDetail> pages = orderDetailService.page(Condition.getPage(query), Condition.getQueryWrapper(orderDetail));
        return R.data(pages);
    }

    /**
     * 新增 订单明细
     */
    @PostMapping("/save")
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "新增", notes = "传入orderDetail")
    public R save(@Valid @RequestBody OrderDetail orderDetail) {
        return R.status(orderDetailService.save(orderDetail));
    }

    /**
     * 修改 订单明细
     */
    @PostMapping("/update")
    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "修改", notes = "传入orderDetail")
    public R update(@Valid @RequestBody OrderDetail orderDetail) {
        return R.status(orderDetailService.updateById(orderDetail));
    }

    /**
     * 新增或修改 订单明细
     */
    @PostMapping("/submit")
    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "新增或修改", notes = "传入orderDetail")
    public R submit(@Valid @RequestBody OrderDetail orderDetail) {
        return R.status(orderDetailService.saveOrUpdate(orderDetail));
    }


    /**
     * 删除 订单明细
     */
    @PostMapping("/remove")
    @ApiOperationSupport(order = 8)
    @ApiOperation(value = "删除", notes = "传入ids")
    public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
        return R.status(orderDetailService.removeByIds(Func.toLongList(ids)));
    }


}
